<?php
/**
 *
 * Base (前台公共模块)
 *
 */
class PageAction extends PublicAction {

    public function index($catid='',$module='')
    {
        //获取路由规则
        $this->Urlrule = F('Urlrule');

        if (empty($catid)) {
            $catid = I('get.id');
        }

        $module = 'Page';

        $p = I('get.p', 1);

        if($catid){
            $cat = $this->categorys[$catid];
            $this->assign('module_name',$module);
            unset($cat['id']);
            $this->assign($cat);
            $cat['id']=$catid;
            $this->assign('catid',$catid);
        }

        $fields = F($this->mod[$module].'_Field');
        foreach($fields as $key=>$r){
            $fields[$key]['setup'] =string2array($fields[$key]['setup']);
        }

        $this->assign( 'fields', $fields);

        $seo_title = $cat['title'] ? $cat['title'] : $cat['catname'];
        $this->assign('seo_title',$seo_title);
        $this->assign('seo_keywords',$cat['keywords']);
        $this->assign('seo_description',$cat['description']);

        $modle = M('Page');

        $data = $modle->find($catid);
        unset($data['id']);

        $fields = F($this->mod[$module].'_Field');
        foreach($data as $key=>$c_d){
            $setup='';
            $fields[$key]['setup'] =$setup=string2array($fields[$key]['setup']);
            if($setup['fieldtype']=='varchar' && $fields[$key]['type']!='text'){
                $data[$key.'_old_val'] =$data[$key];
                $data[$key]=fieldoption($fields[$key],$data[$key]);
            }elseif($fields[$key]['type']=='images' || $fields[$key]['type']=='files'){
                if(!empty($data[$key])){
                    $p_data=explode(':::',$data[$key]);
                    $data[$key]=array();
                    foreach($p_data as $k=>$res){
                        $p_data_arr=explode('|',$res);
                        $data[$key][$k]['filepath'] = $p_data_arr[0];
                        $data[$key][$k]['filename'] = $p_data_arr[1];
                    }
                    unset($p_data);
                    unset($p_data_arr);
                }
            }
            unset($setup);
        }
        $this->assign('fields',$fields);
        //分页
        $CONTENT_POS = strpos($data['content'], '[page]');
        if($CONTENT_POS !== false) {
            $urlrule    = geturl($cat,'',$this->Urlrule);
            $urlrule[0] = urldecode($urlrule[0]);
            $urlrule[1] = urldecode($urlrule[1]);
            $contents   = array_filter(explode('[page]',$data['content']));
            $pagenumber = count($contents);
            for($i=1; $i<=$pagenumber; $i++) {
                $pageurls[$i] = str_replace('{$page}',$i,$urlrule);
            }
            $pages = content_pages($pagenumber,$p, $pageurls);
            //判断[page]出现的位置
            if($CONTENT_POS<7) {
                $data['content'] = $contents[$p];
            } else {
                $data['content'] = $contents[$p-1];
            }
            $this->assign('pages',$pages);
        }

        $template = $cat['wap_template_list'] ? $cat['wap_template_list'] : 'index' ;
        $this->assign($data);

        $this->display('Page:'.$template);
    }


    public function hits()
    {
        $module    = $module ? $module : MODULE_NAME;
        $id        = $id ? $id : intval($_REQUEST['id']);
        $this->db = M($module);
        $this->db->where("id=".$id)->setInc('hits');

        if($module=='Download'){
            $r = $this->db->find($id);
            echo '$("#hits").html('.$r['hits'].');$("#downs").html('.$r['downs'].');';
        }else{
            $hits = $this->db->where("id=".$id)->getField('hits');
            echo '$("#hits").html('.$hits.');';
        }
        exit;
    }
}